package com.yl.ydk.utils

import android.app.AlertDialog
import android.content.Context
import android.text.method.HideReturnsTransformationMethod
import android.text.method.PasswordTransformationMethod
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.RelativeLayout
import android.widget.TextView
import com.yl.ydk.R
import com.yl.ydk.base.BaseApplication
import com.yl.ydk.bean.WifiListBean
import com.yl.ydk.view.CustomDialog
import com.yl.ydk.view.ScaleRulerView
import com.yl.ydk.view.pickerview.DateTimePickerView
import java.text.SimpleDateFormat
import java.util.*


object DialogUtils {


    private var dialog: CustomDialog? = null

    interface OnBtnOkListener {
        fun OnBtnOkListener(item: String?)
    }

    interface OnWifiBtnOkListener {
        fun OnWifiBtnOkListener(item: String?, wifiListBean: WifiListBean?)
    }


    fun showCheckWeight(
        context: Context?, defaultValue: Float,
        onBtnOkListener: OnBtnOkListener
    ) {
        hideDialog()
        var weightValue: String? = defaultValue.toString()
        val view = View.inflate(BaseApplication.getContext(), R.layout.dialog_checkweight, null)
        val scaleWheelView = view.findViewById<ScaleRulerView>(R.id.scaleWheelView)
        val btnOk = view.findViewById<Button>(R.id.btnOk)
        scaleWheelView.initViewParam(defaultValue, 250f, 0f)
        scaleWheelView.setValueChangeListener { value ->
            weightValue = "${value.toInt()}"
        }
        btnOk.setOnClickListener {
            hideDialog()
            onBtnOkListener.OnBtnOkListener(weightValue)
        }
        dialog = CustomDialog(context, view, R.style.MyDialog)
        dialog?.setCancelable(true)
        dialog?.show()

    }

    fun showCheckAge(
        context: Context?, onBtnOkListener: OnBtnOkListener
    ) {
        hideDialog()
        var birthday = getDateString(Calendar.getInstance())
        val view = View.inflate(BaseApplication.getContext(), R.layout.dialog_checkage, null)
        val dateTimePickerView = view.findViewById<DateTimePickerView>(R.id.dateTimePickerView)
        val btnOk = view.findViewById<Button>(R.id.btnOk)

        dateTimePickerView.setStartDate(GregorianCalendar(1877, 6, 27, 21, 30))
        dateTimePickerView.selectedDate = getDateCalendar(birthday)

        dateTimePickerView.setOnSelectedDateChangedListener { date: Calendar? ->
            birthday = getDateString(date!!)
        }

        btnOk.setOnClickListener {
            hideDialog()
            onBtnOkListener.OnBtnOkListener(birthday)
        }

        dialog = CustomDialog(context, view, R.style.MyDialog)
        dialog?.setCancelable(true)
        dialog?.show()

    }


    private fun getDateCalendar(date: String): Calendar = Calendar.getInstance().apply {
        SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).parse(date)?.let { time = it }
    }

    private fun getDateString(date: Calendar): String {
        val year = date[Calendar.YEAR]
        val month = date[Calendar.MONTH] + 1
        val dayOfMonth = date[Calendar.DAY_OF_MONTH]
        return StringBuilder().append(year).append("-")
            .append(month).append("-")
            .append(dayOfMonth).toString()
    }

    fun showCheckSex(
        context: Context?, tvNanClick: View.OnClickListener,
        tvNvClick: View.OnClickListener
    ) {
        hideDialog()
        val view = View.inflate(BaseApplication.getContext(), R.layout.dialog_checksex, null)
        val tvNan = view.findViewById<TextView>(R.id.tvNan)
        val tvNv = view.findViewById<TextView>(R.id.tvNv)
        tvNan.setOnClickListener(tvNanClick)
        tvNv.setOnClickListener(tvNvClick)
        dialog = CustomDialog(context, view, R.style.MyDialog)
        dialog?.setCancelable(true)
        dialog?.show()
    }

    fun showConnectingDialog(context: Context?) {
        hideDialog()
        val view = View.inflate(BaseApplication.getContext(), R.layout.dialog_wificonnectint, null)
        dialog = CustomDialog(context, view, R.style.MyDialog)
        dialog?.setCancelable(true)
        dialog?.show()
    }

    fun showConnectFailDialog(context: Context?){
        hideDialog()
        val view = View.inflate(BaseApplication.getContext(), R.layout.dialog_connectfail, null)
        val tvOK = view.findViewById<TextView>(R.id.tvOK)
        tvOK.setOnClickListener {
            hideDialog()
        }
        dialog = CustomDialog(context, view, R.style.MyDialog)
        dialog?.setCancelable(true)
        dialog?.show()
    }

    /**
     * 隐藏对话框
     */
    fun hideDialog() {
        if (dialog != null && dialog?.isShowing!!) {
            dialog?.dismiss()
            dialog = null
        }
    }

    private var alertDialog: AlertDialog? = null

    fun  dismissAlertDialog(){
        if (alertDialog != null) {
            alertDialog?.dismiss()
            alertDialog = null
        }
    }

    fun showCustomizeDialog(
        context: Context?,
        item: WifiListBean?,
        onWifiBtnOkListener: OnWifiBtnOkListener
    ) {
        dismissAlertDialog()
        var isOpenEye = false
        val builder = AlertDialog.Builder(context)
        alertDialog = builder.create()
        val dialogView: View = LayoutInflater.from(context).inflate(R.layout.dialog_wifiinfo, null)
        val tvSSID = dialogView.findViewById<TextView>(R.id.tvSSID)
        val btnConcel = dialogView.findViewById<Button>(R.id.btnConcel)
        val btnConnect = dialogView.findViewById<Button>(R.id.btnConnect)
        val rlBuXianShi = dialogView.findViewById<RelativeLayout>(R.id.rlBuXianShi)
        val etPassword = dialogView.findViewById<EditText>(R.id.etPassword)
        btnConcel.setOnClickListener {
            dismissAlertDialog()
        }
        btnConnect.setOnClickListener {  //确定
            alertDialog?.dismiss()
            alertDialog = null
            onWifiBtnOkListener.OnWifiBtnOkListener(etPassword.text.toString().trim(), item)
        }
        rlBuXianShi.setOnClickListener {
            if (!isOpenEye) {   //密码可见
                isOpenEye = true
                etPassword.transformationMethod = HideReturnsTransformationMethod.getInstance()
                etPassword.setSelection(etPassword.text.length)
            } else { //密码不可见
                isOpenEye = false
                etPassword.transformationMethod = PasswordTransformationMethod.getInstance()
                etPassword.setSelection(etPassword.text.length)
            }
        }
        tvSSID.text = item?.SSID
        alertDialog?.setView(dialogView)
        alertDialog?.show()
    }

}